<template>
  <div class="ccqk-root">
    <base-table-detail :table-column="tableColumn" :form-filed="formFiled" :mid="mid" :query-list-action="queryListAction" :rules="rules"
                       :submit-data-action="submitDataAction" :submit-before-action="submitBeforeAction" :load-list-before="loadListBefore">

      <template v-slot:form-item>

        <el-form-item label="防治类别" prop="category">
          <el-select v-model="formFiled.category" placeholder="请选择" clearable size="small">
            <el-option
              v-for="dict in categoryDict"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="病虫害情况" prop="type">
          <el-select v-model="formFiled.type" placeholder="请选择" clearable size="small">
            <el-option
              v-for="dict in typeDict"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发生时间" prop="happenDate">
          <el-date-picker
            size="mini"
            v-model="formFiled.happenDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="施药方式" prop="type">
          <el-select v-model="formFiled.weedType" placeholder="请选择" clearable size="small">
            <el-option
              v-for="dict in weedTypeDict"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="除草方式描述" prop="weedDesc">
          <el-input v-model="formFiled.weedDesc"></el-input>
        </el-form-item>
        <el-form-item label="药品名称" prop="pesticideName">
          <el-input v-model="formFiled.pesticideName"></el-input>
        </el-form-item>

        <el-form-item label="防治对象" prop="controlObject">
          <el-input v-model="formFiled.controlObject"></el-input>
        </el-form-item>
        <el-form-item label="施用时期" prop="useData">
          <el-date-picker
            size="mini"
            v-model="formFiled.useData"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="施用面积" prop="useArea">
          <el-input type="number" v-model="formFiled.useArea"></el-input>
        </el-form-item>

        <el-form-item label="施用剂量" prop="useDose">
          <el-input v-model="formFiled.useDose"></el-input>
        </el-form-item>

        <el-form-item label="施用方法" prop="useMethod">
          <el-input v-model="formFiled.useMethod"></el-input>
        </el-form-item>

        <el-form-item label="防治效果" prop="effect">
          <el-select v-model="formFiled.effect" placeholder="请选择" clearable size="small">
            <el-option
              v-for="dict in effectDict"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="效果追踪" prop="effectTracking">
          <el-input v-model="formFiled.effectTracking"></el-input>
        </el-form-item>
        <el-form-item label="是否完成" prop="isComplete">
          <el-radio-group v-model="formFiled.isComplete">
            <el-radio v-for="dict in isCompleteDict" :key="dict.dictValue" :label="dict.dictValue">{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>


        <el-form-item label="完成时间" prop="completeDate" v-show="formFiled.isComplete == 1">
          <el-date-picker
            size="mini"
            v-model="formFiled.completeDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="防治前" prop="fzq">
          <upload-image :limit="1" v-model="formFiled.fzq"></upload-image>
        </el-form-item>
        <el-form-item label="防治过程" prop="fzgc">
          <upload-image :limit="1" v-model="formFiled.fzgc"></upload-image>
        </el-form-item>
        <el-form-item label="防治后" prop="fzh">
          <upload-image :limit="1" v-model="formFiled.fzh"></upload-image>
        </el-form-item>
        <el-form-item label="防治完成" prop="fzwc" v-show="formFiled.isComplete == 1">
          <upload-image :limit="1" v-model="formFiled.fzwc"></upload-image>
        </el-form-item>

      </template>
    </base-table-detail>
  </div>
</template>

<script>
  import { reactive, toRefs, onMounted, ref, watch, computed } from '@vue/composition-api'
  import baseTableDetail from '../components/baseTableDetail'
  import { pestsEdit, pestsList } from '@/api/industry/cpf'
  import { getDicts } from '@/api/system/dict/data'
  import uploadImage from '@/components/UploadImage'

  export default {
    name: 'ccqk',
    props: {
      mid: {
        type: [String, Number],
        default: 1
      }
    },
    components: {
      uploadImage,
      baseTableDetail
    },
    setup(props, content) {
      let data = reactive({
        tableColumn: [
          { name: '防治类别', prop: 'category' },
          { name: '病虫害情况', prop: 'type' },
          { name: '发生时间', prop: 'happenDate' },
          { name: '施药方式', prop: 'weedType' },
          { name: '描述', prop: 'weedDesc' },
          { name: '药品名称', prop: 'pesticideName' },
          { name: '防治对象', prop: 'controlObject' },
          { name: '施用时期', prop: 'useData' },
          { name: '施用面积', prop: 'useArea' },
          { name: '施用剂量', prop: 'useDose' },
          { name: '施用方法', prop: 'useMethod' },
          { name: '效果', prop: 'effect' },
          { name: '效果追踪', prop: 'effectTracking' },
          { name: '是否完成', prop: 'isComplete' },
          { name: '完成时间', prop: 'completeDate' },
          {
            name: '操作',
            prop: 'operation',
            type: 'slot',
            width: 100
          }],
        rules: {},
        queryListAction: pestsList,
        submitDataAction: pestsEdit,
        formFiled: {
          category: null,
          type: null,
          happenDate: null,
          weedType: null,
          weedDesc: null,
          pesticideName: null,
          controlObject: null,
          useData: null,
          useArea: null,
          useDose: null,
          useMethod: null,
          effect: null,
          effectTracking: null,
          isComplete: null,
          completeDate: null,
          fzq: [],
          fzgc: [],
          fzh: [],
          fzwc: []
        },
        categoryDict: [],
        typeDict: [],
        weedTypeDict: [],
        effectDict: [],
        isCompleteDict: []
      })

      const loadDict = async() => {
        let response = await Promise.all([
          getDicts('fil_diseases_category'),
          getDicts('fil_diseases_type'),
          getDicts('fil_weed_type'),
          getDicts('fil_diseases_effect'),
          getDicts('fil_diseases_is_complete')
        ])
        let [{ data: categoryDict },
          { data: typeDict },
          { data: weedTypeDict },
          { data: effectDict },
          { data: isCompleteDict }
        ] = response
        data.categoryDict = categoryDict
        data.typeDict = typeDict
        data.weedTypeDict = weedTypeDict
        data.effectDict = effectDict
        data.isCompleteDict = isCompleteDict

      }

      const loadListBefore = async(response) => {
        await loadDict()
        response.rows.map(item => {
          if (item.category != null) {
            item.category = data.categoryDict.find(el => item.category == el.dictValue).dictLabel
          }
          if (item.type != null) {
            item.type = data.typeDict.find(el => item.type == el.dictValue).dictLabel
          }
          if (item.weedType != null) {
            item.weedType = data.weedTypeDict.find(el => item.weedType == el.dictValue).dictLabel
          }
          if (item.effect != null) {
            console.log(item.effect, data)
            item.effect = data.effectDict.find(el => item.effect == el.dictValue).dictLabel
          }
          if (item.isComplete != null) {
            item.isComplete = data.isCompleteDict.find(el => item.isComplete == el.dictValue).dictLabel
          }
          return item
        })
        return response
      }

      const submitBeforeAction = (requestData) => {
        let req = JSON.parse(JSON.stringify(requestData))
        req.photos = [{ type: 1, ...req.fzq[0] }, { type: 2, ...req.fzgc[0] }, { type: 3, ...req.fzh[0] }]
        if (req.isComplete == 1) {
          req.photos.push({ type: 4, ...req.fzwc[0] })
        } else {
          delete req.completeDate
        }
        return req
      }
      onMounted(() => {
      })
      return { ...toRefs(data), submitBeforeAction, loadListBefore }
    }
  }
</script>

<style scoped lang="scss">
  .bchfzk-root {

  }
</style>
